package com.starhub.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.starhub.dao.StarHubBaseDao;
import com.starhub.dao.TileDao;
import com.starhub.entity.Tile;

public class TileDaoImpl extends StarHubBaseDao implements TileDao {

	public List<Tile> getTiles(int categoryId) {
		
		Session session = sessionFactory.openSession();
		return session.createQuery("from Tile t where t.category.id = :categoryId ORDER BY t.id ASC").setInteger("categoryId", categoryId).list();
	}
	
	public Tile getTileDetails(int tileId) {
		
		Session session = sessionFactory.openSession();
		Tile tile = (Tile) session.get(Tile.class, tileId);
		return tile;
	}

	public void updateTile(Tile tile) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Tile oldTile = (Tile) session.get(Tile.class, tile.getId());
		oldTile.setImage(tile.getImage());
		oldTile.setLink(tile.getLink());
		oldTile.setTitle(tile.getTitle());
		oldTile.setText(tile.getText());
		
		
		session.saveOrUpdate(oldTile);
		tx.commit();
		session.close();
	}
}
